package tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description: 429. N 叉树的层序遍历
 * User: Li_yizYa
 * Date: 2025/5/25
 * Time: 22:30
 */
public class Solution8 {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int len = queue.size();
            List<Integer> tmp = new ArrayList<>();
            while (len > 0) {
                Node node = queue.poll();
                tmp.add(node.val);
                for (Node tmpNode : node.children) {
                    queue.offer(tmpNode);
                }
                len--;
            }
            ret.add(tmp);
        }
        return ret;
    }
}
